﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VirtuOS.Components;
using System.Text.RegularExpressions;
using System.IO;
using VirtuOS.Base;

namespace VirtuOS.Components
{
    class UserHandler
    {
        internal static void SetupUsers()
        {
            Console.Write("Would you like to add a new user? (y/n): ");
            string ans = Console.ReadLine();
            if (Regex.Match(ans, @"y(es)?", RegexOptions.IgnoreCase).Success)
            {
                AddUser();
            }
        }

        internal static void AddUser(string user)
        {
            byte[] salt = HashHandler.CreateSalt();
            string pwd = HashHandler.ComputeHash(InputHandler.Masked("New password for " + user + ":"), "SHA512", salt);
            string pwd_confirm = HashHandler.ComputeHash(InputHandler.Masked("Confirm password for " + user + ":"), "SHA512", salt);
            if (pwd.Equals(pwd_confirm))
            {
                string strsalt = String.Empty;

                for (int i = 0; i < salt.Length; i++)
                {
                    if (i == 0)
                        strsalt = Convert.ToString(salt[i]);
                    else
                        strsalt = strsalt + "-" + salt[i];
                }

                string output = user + ":" + strsalt + ":" + pwd;
                string file = VirtuOS.vdir_full + "/etc/users/passwds";

                using (StreamWriter sw = new StreamWriter(file, true))
                {
                    sw.WriteLine(output);
                    sw.Close();
                }

                Directory.CreateDirectory(VirtuOS.vdir_full + "/home/" + user);
                DefaultFiles(user);
            }
            else
            {
                Console.WriteLine("Passwords do not match.");
                AddUser();
            }
        }

        public static void AddUser()
        {
            Console.Write("New user name: ");
            string user = Console.ReadLine();
            byte[] salt = HashHandler.CreateSalt();
            string pwd = HashHandler.ComputeHash(InputHandler.Masked("New password for " + user + ":"), "SHA512", salt);
            string pwd_confirm = HashHandler.ComputeHash(InputHandler.Masked("Confirm password for " + user + ":"), "SHA512", salt);
            if (pwd.Equals(pwd_confirm))
            {
                string strsalt = String.Empty;

                for (int i = 0; i < salt.Length; i++)
                {
                    if (i == 0)
                        strsalt = Convert.ToString(salt[i]);
                    else
                        strsalt = strsalt + "-" + salt[i];
                }

                string output = user + ":" + strsalt + ":" + pwd;
                string file = VirtuOS.vdir_full + "/etc/users/passwds";

                using (StreamWriter sw = new StreamWriter(file, true))
                {
                    sw.WriteLine(output);
                    sw.Close();
                }

                Directory.CreateDirectory(VirtuOS.vdir_full + "/home/" + user);
                DefaultFiles(user);
            }
            else
            {
                Console.WriteLine("Passwords do not match.");
                AddUser();
            }
        }

        public static void DelUser(string user)
        {
            string file = VirtuOS.vdir_full + "/etc/users/passwds";
            int l;
            if (FileHandler.CountLinesInFile(file) == 0) { l = 1; }
            else { l = Convert.ToInt32(FileHandler.CountLinesInFile(file)); }
            string[] backupdata = new string[l];
            using (StreamReader sr = new StreamReader(file))
            {
                int i = 0;
                while (!sr.EndOfStream)
                {
                    string cl = sr.ReadLine();
                    string[] chk = cl.Split(':');
                    if (!chk[0].Equals(user))
                    {
                        backupdata[i] = cl;
                        i++;
                    }
                }
            }
            File.Delete(file);
            using (StreamWriter sw = new StreamWriter(file))
            {
                for (int i = 0; i < backupdata.Length; i++)
                {
                    if (backupdata[i] != null) { sw.WriteLine(backupdata[i]); }
                }
            }
        }

        internal static void DefaultFiles(string user)
        {
            if (!File.Exists("home/" + user + "/variables.gr"))
            {
                string name = "username=\"" + user + "\"";
                string homedir = "homedir=\"home/" + user + "\"";
                string ps1 = "PS1=\"\\u@\\h \\w: \\$ \"";
                Terminal.ChangeDir(VirtuOS.vdir_full);

                using (StreamWriter sw = new StreamWriter("home/" + user + "/variables.gr", true))
                {
                    sw.WriteLine(name);
                    sw.WriteLine(homedir);
                    sw.WriteLine(ps1);
                }
            }
        }
    }
}
